﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SelectList.aspx.cs" Inherits="HQWeb.Resources.MultiMaterialList.SelectList" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <%--<link href="../../Styles/Site.css" rel="Stylesheet" type="text/css" />--%>
    <link href="../../Styles/GridViewStyle.css" rel="Stylesheet" type="text/css" />
    <script type="text/javascript" language="javascript">

        function fnAddForPage(vID, vName) {
            //获取打开页面选择情况.
            var objID = parent.Tree.document.getElementById('txtitemIDValueHid');
            var objName = parent.Tree.document.getElementById('txtitemNameValueHid');
            //alert(obj.innerHTML);
            //校验添加的选项是否存在
            var vSelectID = objID.value.split(",");
            var vSelectName = objName.value.split(",");
            for (var i = 0; i < vSelectName.length; i++) {
                if (vID == vSelectID[i]) {
                    return;
                }
            }

            //父选择页面添加选项
            fnAdd(vID, vName);
        }

        function fnAddItem(vID, vName) {
            //获取打开页面选择情况.
            var objID = parent.Tree.document.getElementById('txtitemIDValueHid');
            var objName = parent.Tree.document.getElementById('txtitemNameValueHid');
            //alert(obj.innerHTML);
            //校验添加的选项是否存在
            var vSelectID = objID.value.split(",");
            var vSelectName = objName.value.split(",");
            for (var i = 0; i < vSelectName.length; i++) {
                if (vID == vSelectID[i]) {
                    fnRemove(vID, vName);
                    return;
                }
            }

            //父选择页面添加选项
            fnAdd(vID, vName);
        }

        function fnAdd(vID, vName) {
            //获取打开页面选择情况.
            var objID = parent.Tree.document.getElementById('txtitemIDValueHid');
            var objName = parent.Tree.document.getElementById('txtitemNameValueHid');

            var vStrID = objID.value;
            var vStrName = objName.value;
            if (vStrID == "") {
                vStrID = vID;
                vStrName = vName;
            }
            else {
                vStrID = vStrID + "," + vID;
                vStrName = vStrName + "," + vName;
            }
            objID.value = vStrID;
            objName.value = vStrName;
            fnBindSelect();
        }

        function fnRemove(vID, vName) {
            //获取打开页面选择情况.
            var objID = parent.Tree.document.getElementById('txtitemIDValueHid');
            var objName = parent.Tree.document.getElementById('txtitemNameValueHid');

            var vStrID = objID.value;
            var vStrName = objName.value;
            if (vStrID.indexOf(",") == -1) {
                vStrID = "";
                vStrName = "";
            }
            else {
                vStrID = "," + vStrID + ",";
                vStrName = "," + vStrName + ",";
                vStrID = vStrID.replace("," + vID + ",", ",");
                vStrName = vStrName.replace("," + vName + ",", ",");
                vStrID = vStrID.substring(1);
                vStrID = vStrID.substring(0, vStrID.length - 1);
                vStrName = vStrName.substring(1);
                vStrName = vStrName.substring(0, vStrName.length - 1);
            }
            objID.value = vStrID;
            objName.value = vStrName;
            fnBindSelect();
        }

        function fnBindSelect() {
            //获取打开页面选择情况.
            var objIDValue = parent.Tree.document.getElementById('txtitemIDValueHid');
            var objNameValue = parent.Tree.document.getElementById('txtitemNameValueHid');

            if (objIDValue.value == "all") {
                objIDValue.value = "";
            }
            var vSelfSelect = document.getElementById('divSelect');

            var addbtn = document.getElementsByName("add");
            for (var l = 0; l < addbtn.length; l++) {
                addbtn[l].innerHTML = "添加";
            }
            if (objIDValue.value == "") {
                vSelfSelect.innerHTML = "";
                return;
            }
            var vCode = "";
            var vSelectID = objIDValue.value.split(",");
            var vSelectName = objNameValue.value.split(",");
            var objPageIDValue = document.getElementById('txtPageIDValueHid');
            var vPageIDs = objPageIDValue.value.split(",");
            var vIndex = "";
            for (var i = 0; i < vSelectID.length; i++) {
                for (var j = 0; j < vPageIDs.length; j++) {
                    if (vPageIDs[j] == vSelectID[i]) {
                        vIndex += "," + j;
                    }
                }
                vCode = vCode + ",<a href='#' onclick=\"fnRemove('" + vSelectID[i] + "','" + vSelectName[i] + "');\"><span style='color: background;'>" + vSelectName[i] + "</span></a>";
            }
            vCode = vCode.substring(1);
            vSelfSelect.innerHTML = vCode;

            if (vIndex != "") {
                var vIndexs = vIndex.substring(1).split(",");

                for (var k = 0; k < vIndexs.length; k++) {
                    var vRow = vIndexs[k];
                    addbtn[vRow].innerHTML = "<font color=red>移除</font>";
                }
            }
        }

        function fnAddAllItem() {
            var objAllIDValue = document.getElementById('txtPageIDValueHid').value;
            var objAllNameValue = document.getElementById('txtPageNameValueHid').value;
            //校验添加的选项是否存在
            var vIDValue = objAllIDValue.split(",");
            var vNameValue = objAllNameValue.split(",");
            for (var i = 0; i < vIDValue.length; i++) {
                fnAddForPage(vIDValue[i], vNameValue[i]);
            }
        }

        function fnRemoveAllItem() {
            var objAllIDValue = document.getElementById('txtPageIDValueHid').value;
            var objAllNameValue = document.getElementById('txtPageNameValueHid').value;
            //校验添加的选项是否存在
            var vIDValue = objAllIDValue.split(",");
            var vNameValue = objAllNameValue.split(",");
            for (var i = 0; i < vIDValue.length; i++) {
                fnRemove(vIDValue[i], vNameValue[i]);
            }
        }

        function fnAddAll() {
            var objAllIDValue = document.getElementById('txtAllIDValueHid').value;
            var objAllNameValue = document.getElementById('txtAllNameValueHid').value;
            //校验添加的选项是否存在
            var vIDValue = objAllIDValue.split(",");
            var vNameValue = objAllNameValue.split(",");
            for (var i = 0; i < vIDValue.length; i++) {
                fnAddForPage(vIDValue[i], vNameValue[i]);
            }
        }

        function fnRemoveAll() {
            //获取打开页面选择情况.
            var objID = parent.Tree.document.getElementById('txtitemIDValueHid');
            var objName = parent.Tree.document.getElementById('txtitemNameValueHid');
            objID.value = "";
            objName.value = "";
            fnBindSelect();
        }

        function CheckSubmit() {
            var objIDValue = parent.Tree.document.getElementById('txtitemIDValueHid').value;
            var objNameValue = parent.Tree.document.getElementById('txtitemNameValueHid').value;
            var objSign = parent.Tree.document.getElementById('txtSignHid').value;
            if (objIDValue == "") {
                alert("已选择员工为空，请先添加！");
                return;
            }

            if (objSign == "1" && objIDValue.indexOf(",") != -1) {
                alert("只能选择一个员工！");
                return;
            }

            var vReturn = [];

            vReturn[0] = objIDValue;
            vReturn[1] = objNameValue;
            parent.window.returnValue = vReturn;
            parent.window.close();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager" runat="server" />
    <ajaxToolkit:AutoCompleteExtender ID="ajaxtitel" runat="server" TargetControlID="Titel" ServicePath="../../Wbserver/GetSmartTips.asmx" 
    ServiceMethod="GetMaterialName" MinimumPrefixLength="1" ></ajaxToolkit:AutoCompleteExtender> 
    <div>
         <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#9BC2ED">
            <tr bgcolor="white">
                <td style="width: 20%">
                    元素名称
                </td>
                <td>
                    <%--<input runat="server" id="txtSearch" />--%><asp:TextBox ID="Titel" runat="server"></asp:TextBox>&nbsp;
                    <asp:Button ID="btnSearch" runat="server" Text="查询" OnClick="btnSearch_Click" />&nbsp;
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                    <%--<input id="btnSselectAll" type="button" value="全部添加" onclick="fnAddAllItem();" />--%>
                </td>
            </tr>
            <tr bgcolor="white">
                <td colspan="2">
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                        CellPadding="4" DataKeyNames="MATERIAL_ID" EmptyDataText="没有数据记录！！" ForeColor="#333333"
                        OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10" SkinID="GridViewSkin"
                        Width="100%" OnRowDataBound="GridView1_RowDataBound">
                        <Columns>
                            <asp:BoundField DataField="MATERIAL_ID" HeaderText="编号" ItemStyle-Width="0px" />
                            <asp:BoundField DataField="MATERIAL_NAME" HeaderText="元素名称" ItemStyle-HorizontalAlign="Center"
                                ItemStyle-Width="30%" />
                            <asp:BoundField DataField="TYPE_NAME" HeaderText="所属元素分类名称" ItemStyle-HorizontalAlign="Center" />
                            <asp:BoundField DataField="PRICE" HeaderText="价格" ItemStyle-HorizontalAlign="Center"
                                ItemStyle-Width="15%" />
                            <asp:TemplateField HeaderText="操作" ItemStyle-Width="19%" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <a name="add" href="#" style="color: background;" onclick="Javascript:fnAddItem('<%# Eval("MATERIAL_ID") %>','<%# Eval("MATERIAL_NAME") %>')">
                                        添加</a>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <PagerTemplate>
                            <table width="100%">
                                <tr>
                                    <td style="text-align: center">
                                        第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />页
                                        共<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount  %>' />页
                                        <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
                                            CommandName="Page" Text="首页" ForeColor="White" />
                                        <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
                                            CommandName="Page" Text="上一页" ForeColor="White" />
                                        <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next"
                                            CommandName="Page" Text="下一页" ForeColor="White" />
                                        <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last"
                                            CommandName="Page" Text="尾页" ForeColor="White" />
                                        <asp:TextBox ID="txtNewPageIndex" runat="server" Width="28px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />
                                        <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"
                                            CommandName="Page" Text="GO" ForeColor="White" />
                                    </td>
                                </tr>
                            </table>
                        </PagerTemplate>
                        <FooterStyle BackColor="#9BC2ED" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#EFF3FB" />
                        <EditRowStyle BackColor="#73ACEC" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#73ACEC" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#9BC2ED" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>
                </td>
            </tr>
            <tr bgcolor="white">
                <td colspan="2">
                    <a href="#" onclick="Javascript:fnAddAllItem();" style="color: background;">本页添加</a>
                    &nbsp; &nbsp; <a href="#" onclick="Javascript:fnRemoveAllItem();" style="color: background;">
                        本页移除</a> &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" onclick="Javascript:fnAddAll();"><span
                            style="color: background;">全部添加</span></a>
                </td>
            </tr>
            <tr bgcolor="white">
                <td style="width: 20%">
                    已选择元素
                </td>
                <td>
                    <div id="divSelect" runat="server" style="width: 100%; height: 50px; overflow: auto">
                    </div>
                </td>
            </tr>
            <tr bgcolor="white">
                <td colspan="2" align="center">
                    <input type="button" value="确定" onclick="javascript:CheckSubmit();" />
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="button" value="清空" onclick="Javascript:fnRemoveAll();" />
                </td>
            </tr>
        </table>
        <table>
            <tr>
                <td>
                    <input id="txtitemNameValueHid" runat="server" style="visibility: hidden; width: 1px;"
                        title="父控件选择名称框值" />
                    <input id="txtObjTreeIDHid" runat="server" style="visibility: hidden; width: 1px;"
                        title="父控件已选项id记录" />
                    <input id="txtSearchHid" runat="server" style="visibility: hidden; width: 1px;" title="父控件已选项id记录" />
                    <input id="txtPageIDValueHid" runat="server" style="visibility: hidden; width: 1px;"
                        title="列表中本页ID组" />
                    <input id="txtPageNameValueHid" runat="server" style="visibility: hidden; width: 1px;"
                        title="列表中本页名称组" />
                    <input id="txtAllIDValueHid" runat="server" style="visibility: hidden; width: 1px;"
                        title="列表中所有ID组" />
                    <input id="txtAllNameValueHid" runat="server" style="visibility: hidden; width: 1px;"
                        title="列表中所有名称组" />
                         <input id="txtSign" runat="server" style="visibility: hidden; width: 1px;"
                        title="步骤ID" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
